using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Admins {
	class AdminDA {

		private const string SP_Insert = "dbo.Admin_Insert";
		private const string SP_Login = "dbo.Admin_Login";
		private const string SP_UpdateLastLogin = "dbo.Admin_UpdateLastLogin";
		private const string SP_UpdatePassword = "dbo.Admin_UpdatePassword";
        private const string SP_UpdateRights = "dbo.Admin_UpdateRights";
		private const string SP_UpdateType = "dbo.Admin_UpdateType";

		public static AdminEnt CreateFromReader(IDataReader reader) {
			AdminEnt item = new AdminEnt();
			item.UserName = ConvertUtils.ToString(reader["UserName"]);
			item.Password = ConvertUtils.ToString(reader["Password"]);
			item.Name = ConvertUtils.ToString(reader["Name"]);
			item.Type = (EAdminType)ConvertUtils.ToByte(reader["Type"]);
            item.Rights = (EAdminRight)ConvertUtils.ToByte(reader["Rights"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
			item.LastLogin = ConvertUtils.ToDateTime(reader["LastLogin"]);
			item.DateModified = ConvertUtils.ToDateTime(reader["DateModified"]);
			return item;
		}

        public static AdminEnt Login(string userName, string password, DbConnection dbCon, DbTransaction dbTrans) {
            AdminEnt result = null;
            Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_Login)) {
                db.AddParameter(cmd, "UserName", userName);
                db.AddParameter(cmd, "Password", password);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = CreateFromReader(reader);
                    }
                }
            }
            return result;
        }

		public static int Insert(AdminEnt item, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameter(cmd, "UserName", item.UserName);
				db.AddParameter(cmd, "Password", item.Password);
				db.AddParameter(cmd, "Name", item.Name);
				db.AddParameter(cmd, "Type", item.Type);
                db.AddParameter(cmd, "Rights", item.Rights);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);
				db.AddParameter(cmd, "LastLogin", item.LastLogin);
				db.AddParameter(cmd, "DateModified", item.DateModified);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

		public static int UpdateLastLogin(AdminEnt item, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_UpdateLastLogin)) {
				db.AddParameter(cmd, "UserName", item.UserName);
				db.AddParameter(cmd, "LastLogin", item.LastLogin);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

		public static int UpdatePassword(AdminEnt item, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_UpdatePassword)) {
				db.AddParameter(cmd, "UserName", item.UserName);
				db.AddParameter(cmd, "Password", item.Password);
				db.AddParameter(cmd, "DateModified", item.DateModified);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

        public static int UpdateRights(AdminEnt item, DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_UpdateRights)) {
                db.AddParameter(cmd, "UserName", item.UserName);
                db.AddParameter(cmd, "Rights", item.Rights);
                db.AddParameter(cmd, "DateModified", item.DateModified);
                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                }
            }
            return result;
        }

		public static int UpdateType(AdminEnt item, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(ConfigInfo.ConnectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_UpdateType)) {
				db.AddParameter(cmd, "UserName", item.UserName);
				db.AddParameter(cmd, "Type", item.Type);
				db.AddParameter(cmd, "DateModified", item.DateModified);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

	}
}